using System.Windows.Forms;
using Infragistics.UltraChart.Resources.Appearance;

namespace CustomChart
{
	public class ThresholdNormalChart : NormalChart
	{
		public delegate void DelegateOnThresholdChangedEvent(object sender, SelectedSeriesChangedArgs e);

		private XYSeriesEx _ThresholdSeries;

		private bool mouseDown;

		public bool CanDragThreshold { get; set; }

		public event DelegateOnThresholdChangedEvent OnThresholdChanged;

		private void RaiseThresholdChanged(XYSeriesEx series)
		{
			if (this.OnThresholdChanged != null)
			{
				this.OnThresholdChanged(this, new SelectedSeriesChangedArgs
				{
					SelectedSeries = series
				});
			}
		}

		protected override void Chart_MouseDown(object sender, MouseEventArgs e)
		{
			base.Chart_MouseDown(sender, e);
			if (CanDragThreshold)
			{
				mouseDown = true;
			}
		}

		protected override void Chart_MouseUp(object sender, MouseEventArgs e)
		{
			base.Chart_MouseUp(sender, e);
			if (CanDragThreshold)
			{
				if (mouseDown && _ThresholdSeries != null)
				{
					SetCursor(Cursors.Arrow);
					RaiseThresholdChanged(_ThresholdSeries);
					_ThresholdSeries = null;
					base.CanScale = true;
					DisplayAnnotation(false);
				}
				mouseDown = false;
			}
		}

		protected override void Chart_MouseMove(object sender, MouseEventArgs e)
		{
			base.Chart_MouseMove(sender, e);
			if (!CanDragThreshold)
			{
				return;
			}
			if (!mouseDown && base.SelectedSerie == null)
			{
				SetCursor(Cursors.Arrow);
				_ThresholdSeries = null;
				base.CanScale = true;
				DisplayAnnotation(false);
				return;
			}
			if (_ThresholdSeries == null && ThresholdLayer.Series.Contains(base.SelectedSerie))
			{
				_ThresholdSeries = (XYSeriesEx)base.SelectedSerie;
				DisplayAnnotation();
				SetAnnotationText(FormatY(GetLocationYData(e.Y)));
				SetCursor(Cursors.HSplit);
				base.CanScale = false;
			}
			if (!mouseDown || _ThresholdSeries == null)
			{
				return;
			}
			double locationYData = GetLocationYData(e.Y);
			foreach (XYDataPoint point in _ThresholdSeries.Points)
			{
				point.ValueY = locationYData;
			}
			SetAnnotationText(FormatY(locationYData));
		}
	}
}
